local shifu = fk.CreateSkill {
  name = "th_shifu",
}

local spec = { ---@type TrigSkelSpec<fun(self: TriggerSkill, event: RoundEvent|PhaseEvent, target: ServerPlayer, player: ServerPlayer, data: RoundData|PhaseData):any>
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = room:getOtherPlayers(player, false)
    local tos = room:askToChoosePlayers(player, {
      targets = targets,
      min_num = 1,
      max_num = 1,
      prompt = "#th_shifu-choose",
      skill_name = shifu.name
    })
    if #tos == 1 then
      event:setCostData(self, { tos = tos })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local dat = event:getCostData(self)
    local to = dat.tos[1]
    room:addTableMark(to, "th_shifu", player)
    room:setPlayerMark(player, "@th_shifu", to)
    local tohand = #to:getCardIds("h")
    local playerhand = #player:getCardIds("h")
    if tohand > playerhand then
      player:drawCards(1, shifu.name)
    elseif tohand < playerhand then
      to:drawCards(1, shifu.name)
    end
    room:handleAddLoseSkills(to, "th_shifu&", nil, false, true)
  end,
}

shifu:addLoseEffect(function(self, player, is_death)
  local room = player.room
  local to = player:getMark("@th_shifu")
  if to ~= 0 then
    room:removeTableMark(to, "th_shifu", player)
    room:setPlayerMark(player, "@th_shifu", 0)
    room:handleAddLoseSkills(to, "-th_shifu&", nil, false, true)
  end
end)

shifu:addEffect(fk.GameStart, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(shifu.name)
  end,
  on_cost = spec.on_cost,
  on_use = spec.on_use,
})
shifu:addEffect(fk.EventPhaseProceeding, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(shifu.name) and player.phase == Player.Start
  end,
  on_cost = spec.on_cost,
  on_use = spec.on_use,
})

local lose = {
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:getMark("@th_shifu") ~= 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = player:getMark("@th_shifu")
    if to then
      room:removeTableMark(to, "th_shifu", player)
      room:setPlayerMark(player, "@th_shifu", 0)
      room:handleAddLoseSkills(to, "-th_shifu&", nil, false, true)
    end
  end,
}

shifu:addEffect(fk.TurnStart, lose)

Fk:loadTranslationTable{
  ["th_shifu"] = "式符",
  [":th_shifu"] = "游戏开始时或准备阶段，你可以选择一名其他角色并令你与其手牌较少的角色摸一张牌，直到你的下个回合开始，其需要使用或打出【杀】或【闪】时，你可以打出一张基本牌并摸一张牌，视为其使用或打出需要的牌。",

  ["#th_shifu-choose"] = "式符：请选择要与“式符”联动的角色，你与其手牌较少的角色摸一张牌",
  ["@th_shifu"] = "式符",
}

return shifu